---@diagnostic disable: lowercase-global, undefined-global, trailing-space

-- Hook为pono角色添加溺水监听
AddPrefabPostInit("pono", function(inst)
    -- 【Hook CollectSceneActions 阻止溺水时的交互】
    local old_CollectSceneActions = inst.CollectSceneActions
    if old_CollectSceneActions then
        inst.CollectSceneActions = function(self, doer, actions, right, ...)
            -- 如果在溺水忙碌状态，不允许任何交互
            if self:HasTag("pono_drowning_busy") then
                print("[Drowning] Blocking actions during drowning")
                return
            end
            
            -- 否则正常收集动作
            return old_CollectSceneActions(self, doer, actions, right, ...)
        end
    end
    
    -- 【客户端】监听溺水开始事件 - 禁用控制
    inst:ListenForEvent("pono_drowning_start", function(inst)
        if not TheNet:IsDedicated() and ThePlayer == inst then
            print("[Drowning Client] Disabling player control and movement")
            
            -- 1. 禁用玩家控制器（最高优先级）
            if ThePlayer.components.playercontroller then
                ThePlayer.components.playercontroller:Enable(false)
                print("[Drowning Client] Player controller disabled")
            end
            
            -- 2. 强制停止当前移动并禁用
            if ThePlayer.components.locomotor then
                -- 立即停止移动
                ThePlayer.components.locomotor:Stop()
                ThePlayer.components.locomotor:Clear()
                -- 设置速度为0
                ThePlayer.components.locomotor:SetExternalSpeedMultiplier(ThePlayer, "drowning", 0)
                print("[Drowning Client] Movement stopped and disabled")
            end
            
            -- 3. 如果在移动状态，强制切换到idle
            if ThePlayer.sg and not ThePlayer.sg:HasStateTag("idle") then
                ThePlayer.sg:GoToState("idle")
                print("[Drowning Client] Forced to idle state")
            end
            
            -- 4. 添加标签阻止状态转换
            ThePlayer:AddTag("busy")
            ThePlayer:AddTag("pono_drowning_busy")
            print("[Drowning Client] Added busy tags")
            
            -- 5. 黑屏效果（立即黑屏，3秒后渐变恢复）
            if ThePlayer.ScreenFade then
                -- false = 淡出到黑屏, 0 = 立即完成
                ThePlayer:ScreenFade(true, 0)
                print("[Drowning Client] Screen faded to black immediately")
                
                -- 3秒后渐变恢复
                ThePlayer:DoTaskInTime(3.0, function()
                    if ThePlayer and ThePlayer:IsValid() and ThePlayer.ScreenFade then
                        -- true = 淡入恢复, 1 = 用1秒渐变
                        ThePlayer:ScreenFade(false, 1)
                        print("[Drowning Client] Screen fading back over 1 second")
                    end
                end)
            end
        end
    end)
    
    -- 【客户端】监听溺水结束事件 - 恢复控制
    inst:ListenForEvent("pono_drowning_end", function(inst)
        if not TheNet:IsDedicated() and ThePlayer == inst then
            print("[Drowning Client] Restoring player control and movement")
            
            -- 1. 移除标签
            ThePlayer:RemoveTag("busy")
            ThePlayer:RemoveTag("pono_drowning_busy")
            print("[Drowning Client] Removed busy tags")
            
            -- 2. 恢复移动
            if ThePlayer.components.locomotor then
                ThePlayer.components.locomotor:RemoveExternalSpeedMultiplier(ThePlayer, "drowning")
                print("[Drowning Client] Movement speed restored")
            end
            
            -- 3. 恢复玩家控制器
            if ThePlayer.components.playercontroller then
                ThePlayer.components.playercontroller:Enable(true)
                print("[Drowning Client] Player controller enabled")
            end
        end
    end)
    
    -- 【客户端】监听wakeup事件 - 在所有环境下都注册
    inst:ListenForEvent("pono_drowning_wakeup", function(inst, data)
        print("[Drowning Client] Wakeup event received!")
        
        if not TheNet:IsDedicated() then
            local player = ThePlayer
            if player and player == inst then
                print("[Drowning Client] Playing wakeup animation")
                
                -- 播放wakeup动画
                if player.sg then
                    if player.sg.sg.states.wakeup then
                        player.sg:GoToState("wakeup")
                        print("[Drowning Client] Wakeup animation started")
                    else
                        player.sg:GoToState("idle")
                        print("[Drowning Client] No wakeup state, going to idle")
                    end
                else
                    print("[Drowning Client] ERROR: sg not found!")
                end
            end
        end
    end)
    
    -- 【服务器端】监听溺水事件
    if TheWorld.ismastersim then
        inst:ListenForEvent("pono_trigger_drown", function(inst, data)
            if inst.components.mod_pono_drowning then
                inst.components.mod_pono_drowning:TriggerDrown(data)
            end
        end)
    end
end)
